/*
 * SMPPTranscView.java
 * SMPP Transceiver
 * Developed by Motaz Abdel Azeem http://code.sd
 * 2011-October 2012-July
 */

package smpptester;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.Properties;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public final class SMPPTesterView extends FrameView {

    SMPPClient mySMPP = null;

    
    public SMPPTesterView(SingleFrameApplication app) {
        super(app);

        initComponents();
        Memo.myTextArea = jTextArea1;
        jTextArea1.append(Charset.defaultCharset().toString() + "\n");
        

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
           //     statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
      //          statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
 //       statusAnimationLabel.setIcon(idleIcon);
     //   progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                   //     statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    //progressBar.setVisible(true);
                    //progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
              //      statusAnimationLabel.setIcon(idleIcon);
                   // progressBar.setVisible(false);
                   // progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                //    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                   // progressBar.setVisible(true);
                   // progressBar.setIndeterminate(false);
                  //  progressBar.setValue(value);
                }
            }
        });
        
        
        Properties properties = new Properties();
        
        
        try 
        {
            properties.load(new FileInputStream("smpptester.properties"));
            jtServer.setText(properties.getProperty("smppServer"));
            jtPort.setText(properties.getProperty("smppPort"));
            jtUser.setText(properties.getProperty("smppUser"));
            jtMDN.setText(properties.getProperty("mdn"));
            jtSourceMDN.setText(properties.getProperty("sourceMDN"));
            jtMsg.setText(properties.getProperty("msg"));
            
        }
        catch (Exception ex)
        {
            System.out.println("Error reading property file: " + ex.toString());
        }

        
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = SMPPTesterApp.getApplication().getMainFrame();
            aboutBox = new SMPPTesterAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        SMPPTesterApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jbStart = new javax.swing.JButton();
        jlTitle = new javax.swing.JLabel();
        jbStop = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jState = new javax.swing.JLabel();
        jtServer = new javax.swing.JTextField();
        jlTitle1 = new javax.swing.JLabel();
        jtPort = new javax.swing.JTextField();
        jlTitle2 = new javax.swing.JLabel();
        jlTitle3 = new javax.swing.JLabel();
        jtUser = new javax.swing.JTextField();
        jpPassword = new javax.swing.JPasswordField();
        jlTitle4 = new javax.swing.JLabel();
        jtMDN = new javax.swing.JTextField();
        jlTitle5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jtSourceMDN = new javax.swing.JTextField();
        jlTitle6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtMsg = new javax.swing.JTextArea();
        jxRX = new javax.swing.JCheckBox();
        jxTX = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 357, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(smpptester.SMPPTesterApp.class).getContext().getResourceMap(SMPPTesterView.class);
        jbStart.setIcon(resourceMap.getIcon("jbStart.icon")); // NOI18N
        jbStart.setText(resourceMap.getString("jbStart.text")); // NOI18N
        jbStart.setName("jbStart"); // NOI18N
        jbStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStartActionPerformed(evt);
            }
        });

        jlTitle.setBackground(resourceMap.getColor("jlTitle.background")); // NOI18N
        jlTitle.setText(resourceMap.getString("jlTitle.text")); // NOI18N
        jlTitle.setName("jlTitle"); // NOI18N

        jbStop.setIcon(resourceMap.getIcon("jbStop.icon")); // NOI18N
        jbStop.setText(resourceMap.getString("jbStop.text")); // NOI18N
        jbStop.setEnabled(false);
        jbStop.setName("jbStop"); // NOI18N
        jbStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStopActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jState.setBackground(resourceMap.getColor("jlState.background")); // NOI18N
        jState.setForeground(resourceMap.getColor("jlState.foreground")); // NOI18N
        jState.setText(resourceMap.getString("jlState.text")); // NOI18N
        jState.setName("jlState"); // NOI18N

        jtServer.setText(resourceMap.getString("jtServer.text")); // NOI18N
        jtServer.setName("jtServer"); // NOI18N

        jlTitle1.setBackground(resourceMap.getColor("jlTitle1.background")); // NOI18N
        jlTitle1.setText(resourceMap.getString("jlTitle1.text")); // NOI18N
        jlTitle1.setName("jlTitle1"); // NOI18N

        jtPort.setText(resourceMap.getString("jtPort.text")); // NOI18N
        jtPort.setName("jtPort"); // NOI18N

        jlTitle2.setBackground(resourceMap.getColor("jlTitle2.background")); // NOI18N
        jlTitle2.setText(resourceMap.getString("jlTitle2.text")); // NOI18N
        jlTitle2.setName("jlTitle2"); // NOI18N

        jlTitle3.setBackground(resourceMap.getColor("jlTitle3.background")); // NOI18N
        jlTitle3.setText(resourceMap.getString("jlTitle3.text")); // NOI18N
        jlTitle3.setName("jlTitle3"); // NOI18N

        jtUser.setText(resourceMap.getString("jtUser.text")); // NOI18N
        jtUser.setName("jtUser"); // NOI18N

        jpPassword.setText(resourceMap.getString("jpPassword.text")); // NOI18N
        jpPassword.setName("jpPassword"); // NOI18N

        jlTitle4.setBackground(resourceMap.getColor("jlTitle4.background")); // NOI18N
        jlTitle4.setText(resourceMap.getString("jlTitle4.text")); // NOI18N
        jlTitle4.setName("jlTitle4"); // NOI18N

        jtMDN.setText(resourceMap.getString("jtMDN.text")); // NOI18N
        jtMDN.setName("jtMDN"); // NOI18N

        jlTitle5.setBackground(resourceMap.getColor("jlTitle5.background")); // NOI18N
        jlTitle5.setText(resourceMap.getString("jlTitle5.text")); // NOI18N
        jlTitle5.setName("jlTitle5"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendSMS(evt);
            }
        });

        jtSourceMDN.setText(resourceMap.getString("jtSourceMDN.text")); // NOI18N
        jtSourceMDN.setName("jtSourceMDN"); // NOI18N

        jlTitle6.setBackground(resourceMap.getColor("jlTitle6.background")); // NOI18N
        jlTitle6.setText(resourceMap.getString("jlTitle6.text")); // NOI18N
        jlTitle6.setName("jlTitle6"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jtMsg.setColumns(20);
        jtMsg.setRows(5);
        jtMsg.setName("jtMsg"); // NOI18N
        jScrollPane2.setViewportView(jtMsg);

        jxRX.setText(resourceMap.getString("jxRX.text")); // NOI18N
        jxRX.setName("jxRX"); // NOI18N

        jxTX.setSelected(true);
        jxTX.setText(resourceMap.getString("jxTX.text")); // NOI18N
        jxTX.setName("jxTX"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jbStart)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbStop, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jlTitle)
                                    .addComponent(jlTitle1))
                                .addGap(18, 18, 18)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtServer, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(25, 25, 25)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlTitle2)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jlTitle3))))
                        .addGap(55, 55, 55)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jpPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                                .addComponent(jtUser))
                            .addComponent(jState)))
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(jlTitle4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtMDN, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(jlTitle6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtSourceMDN, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(161, 161, 161))
                                    .addComponent(jlTitle5, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jxRX)
                    .addComponent(jxTX))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jlTitle2)
                                    .addComponent(jtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jxRX))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(jlTitle3)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel1))
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jxTX))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jState))))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jlTitle)
                                    .addComponent(jtServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jlTitle1)
                                    .addComponent(jtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jbStart, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jbStop, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jlTitle4)
                                    .addComponent(jtMDN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jlTitle5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jtSourceMDN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jlTitle6))
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(smpptester.SMPPTesterApp.class).getContext().getActionMap(SMPPTesterView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void jbStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStartActionPerformed
        // TODO add your handling code here:
        if (mySMPP == null)
        {
            mySMPP = new SMPPClient(jtServer.getText(), 
                    Integer.valueOf(jtPort.getText()),
                    jtUser.getText(), jpPassword.getText(), jxRX.isSelected(), 
                    jxTX.isSelected());
            ResultRec bindResult = mySMPP.bind();
                    
            
            // Connection error
            if (bindResult.resultCode != 0)
            jTextArea1.append(bindResult.resultMsg);
            
            // Connected
            if (bindResult.resultCode == 0) {
                Memo.myTextArea.append("Connected to: " + jtServer.getText());
                jState.setText("Connected");
                jState.setForeground(Color.BLUE);
                mySMPP.receive();
                jbStart.setEnabled(false);
                jbStop.setEnabled(true);
                
                
            }
            
        }
        else // Already bound
        {
            if (mySMPP.checkIsBound()) 
            {
              jTextArea1.append("Session is still opened\n");
            }
            else
            {   mySMPP.unbind();
                mySMPP.init(jtServer.getText(), 
                    Integer.valueOf(jtPort.getText()),
                    jtUser.getText(), jpPassword.getText(), jxRX.isSelected(),
                    jxTX.isSelected());
              ResultRec bindResult = mySMPP.bind();
              jTextArea1.append(bindResult.resultMsg);
              if (bindResult.resultCode != 0)
                jbStart.setEnabled(false);
                jbStop.setEnabled(true);
                mySMPP.receive();
                jState.setText("Connected");
                jState.setForeground(Color.BLUE);
                jTextArea1.append("Connected\n");
             
            }
        }
    }//GEN-LAST:event_jbStartActionPerformed

    private void jbStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStopActionPerformed
        try {
            jState.setText("Disconnected");
            jState.setForeground(Color.red);
            
            if (mySMPP != null)
            {
               mySMPP.unbind();
               mySMPP = null;
               jTextArea1.append("Disconnected\n");
            }
            else jTextArea1.append("Already disconnected");
            jbStart.setEnabled(true);
            jbStop.setEnabled(false);
        } catch (Exception ex) 
        {
            jTextArea1.append(ex.toString() + "\n");
        }
        mySMPP = null;
                Properties properties = new Properties();
                
                properties.setProperty("smppServer", jtServer.getText());
                properties.setProperty("smppPort", jtPort.getText());
                properties.setProperty("smppUser", jtUser.getText());
                properties.setProperty("mdn", jtMDN.getText());
                properties.setProperty("sourceMDN", jtSourceMDN.getText());
                properties.setProperty("msg", jtMsg.getText());
                
                try {
                    properties.save(new FileOutputStream("smpptester.properties"), null);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SMPPTesterView.class.getName()).log(Level.SEVERE, null, ex);
                }
        
    }//GEN-LAST:event_jbStopActionPerformed

    private void SendSMS(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendSMS
        // TODO add your handling code here:
        try {
        if (mySMPP != null) {
            ResultRec res;
            res = (mySMPP.sendTextMessage(jtSourceMDN.getText(), jtMDN.getText(), 
                    jtMsg.getText()));
            if (res.resultCode == 0) {
             jTextArea1.append("Message sent, MessageID = " + res.resultMsg + "\n");
            }
            else {
             jTextArea1.append("Error sending message: " + res.resultMsg + "\n");
            }
                
            
        }
        } catch (Exception e){
            System.out.println("Error in unicode: " + e.toString());
        }
    }//GEN-LAST:event_SendSMS

    public void insertReceivedMsg(String fromMDN, String shortMsg)
    {
       jTextArea1.append("Received: " + fromMDN + ":" + shortMsg);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel jState;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton jbStart;
    private javax.swing.JButton jbStop;
    private javax.swing.JLabel jlTitle;
    private javax.swing.JLabel jlTitle1;
    private javax.swing.JLabel jlTitle2;
    private javax.swing.JLabel jlTitle3;
    private javax.swing.JLabel jlTitle4;
    private javax.swing.JLabel jlTitle5;
    private javax.swing.JLabel jlTitle6;
    private javax.swing.JPasswordField jpPassword;
    private javax.swing.JTextField jtMDN;
    private javax.swing.JTextArea jtMsg;
    private javax.swing.JTextField jtPort;
    private javax.swing.JTextField jtServer;
    private javax.swing.JTextField jtSourceMDN;
    private javax.swing.JTextField jtUser;
    private javax.swing.JCheckBox jxRX;
    private javax.swing.JCheckBox jxTX;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
